package com.piece.mall.api.dto;

import com.piece.core.framework.util.collection.CollectionUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

@Data
@NoArgsConstructor
@ApiModel(value = "购物车数据")
public class CartDTO implements Serializable {

    @ApiModelProperty(value = "购物项集合")
    private List<CartItemDTO> items;

    @ApiModelProperty(value = "商品件数")
    private Integer countNum;

    @ApiModelProperty(value = "商品数量")
    private Integer countType;

    @ApiModelProperty(value = "商品总价")
    private BigDecimal totalAmount;

    @ApiModelProperty(value = "减免价格")
    private BigDecimal reduce = new BigDecimal("0.00");

    public Integer getCountNum() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItemDTO item : items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public Integer getCountType() {
        return CollectionUtil.isEmpty(items) ? 0 : items.size();
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        // 1、计算购物项总价
        if (!CollectionUtil.isEmpty(items)) {
            for (CartItemDTO cartItem : items) {
                if (cartItem.getCheck()) {
                    amount = amount.add(cartItem.getTotalPrice());
                }
            }
        }
        // 2、计算优惠后的价格
        return amount.subtract(getReduce());
    }
}
